package bpm.dao.impl.document;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import bpm.dao.document.DocumentNoteDao;
import bpm.domain.document.DocumentNote;
/**
 * 
 * @author ZhaoWangLong Jul 26, 2011
 *
 */
@Repository("documentNoteDao")
public class DocumentNoteDaoImpl implements DocumentNoteDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	private Logger logger=Logger.getLogger(DocumentNoteDaoImpl.class);
	public void delete(String[] docNoteId) {
		StringBuffer sql=new StringBuffer();
		sql.append(" delete from BPM_DC_NOTE where ID in ( ");
		for (int i = 0; i < docNoteId.length; i++) {
			if(i!=docNoteId.length-1)
			sql.append("?,");
			else
				sql.append("?)");
		}
		logger.debug("DeleteSQL:"+sql.toString());
		this.jdbcTemplate.update(sql.toString(),docNoteId);
	}

	public DocumentNote findNoteById(String noteId) {
		String sql="select ID,WORKITEM_ID, DOC_ID, FIELD_KEY, VALUE, AUTHOR_ID, AUTHOR_NAME,DEPT_ID, DEPT_NAME, CREATE_TIME, LAST_UPDATE_TIME, DESCRIPTION,SCOPE "+
		" from BPM_DC_NOTE where ID=?";
		logger.debug("QuerySQL:"+sql);
		DocumentNote docNote;
		try {
			docNote=(DocumentNote)this.jdbcTemplate.queryForObject(sql, new Object[]{noteId},new DocumentNoteRowMapper());
		} catch (Exception e) {
			docNote=new DocumentNote();
		}
		return docNote;
	}
	@SuppressWarnings("unchecked")
	public List<DocumentNote> findNotesByDocId(String docId) {
		String sql="select ID, WORKITEM_ID,DOC_ID, FIELD_KEY, VALUE, AUTHOR_ID, AUTHOR_NAME,DEPT_ID, DEPT_NAME, CREATE_TIME, LAST_UPDATE_TIME, DESCRIPTION,SCOPE "+
		     " from BPM_DC_NOTE where DOC_ID=? order by FIELD_KEY asc ,LAST_UPDATE_TIME desc  ";
		logger.debug("QuerySQL:"+sql);
		return this.jdbcTemplate.query(sql, new Object[]{docId},new DocumentNoteRowMapper());
	}

	public void insert(DocumentNote docNote) {
		String sql="insert into BPM_DC_NOTE(ID,WORKITEM_ID, DOC_ID, FIELD_KEY, VALUE, AUTHOR_ID, AUTHOR_NAME,DEPT_ID, DEPT_NAME, CREATE_TIME, LAST_UPDATE_TIME, DESCRIPTION,SCOPE )"+
		" values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
		logger.debug("InsertSQL:"+sql);
		this.jdbcTemplate.update(sql,new Object[]{
			docNote.getId(),docNote.getWorkItemId(),
			docNote.getDocumentId(),docNote.getFieldKey(),docNote.getValue(),
			docNote.getAuthorId(),docNote.getAuthorName(),docNote.getDeptId(),docNote.getDeptName(),
			new java.sql.Date(docNote.getCreateTime().getTime()),new java.sql.Date(docNote.getLastUpdateTime().getTime()),
			docNote.getDescription(),
			docNote.getScope()
		});
	}

	public void update(DocumentNote docNote) {
		StringBuffer sql=new StringBuffer();
		sql.append(" update BPM_DC_NOTE set DOC_ID=?,FIELD_KEY=?,VALUE=?,AUTHOR_ID=?,AUTHOR_NAME=?,DEPT_ID=?,DEPT_NAME=?,LAST_UPDATE_TIME=?,DESCRIPTION=?")
		.append(" where ID=? ");
		logger.debug("UpdateSQL:"+sql.toString());
		this.jdbcTemplate.update(sql.toString(),new Object[]{
			docNote.getDocumentId(),docNote.getFieldKey(),docNote.getValue(),
			docNote.getAuthorId(),docNote.getAuthorName(),docNote.getDeptId(),docNote.getDeptName(),
			new java.sql.Date(docNote.getLastUpdateTime().getTime()),
			docNote.getDescription(),docNote.getId()
		});
	}

	public void insert(List<DocumentNote> docNotes) {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into BPM_DC_NOTE(ID,WORKITEM_ID, DOC_ID, FIELD_KEY, VALUE, AUTHOR_ID, AUTHOR_NAME,DEPT_ID, DEPT_NAME, CREATE_TIME, LAST_UPDATE_TIME, DESCRIPTION,SCOPE )")
		.append(" values (?,?,?,?,?,?,?,?,?,?,?,?,?)");
		logger.debug("UpdateSQL:"+sql.toString());
		this.jdbcTemplate.batchUpdate(sql.toString(), new DocumentNoteBatchPreparedStatement(docNotes));
	}

	public void delete(String docId) {
		String sql="delete from BPM_DC_NOTE where DOC_ID=?";
		this.jdbcTemplate.update(sql,new Object[]{docId});
	}

	public void update(List<DocumentNote> docNotes) {
		StringBuffer sql=new StringBuffer();
		sql.append(" update BPM_DC_NOTE set DOC_ID=?,FIELD_KEY=?,VALUE=?,AUTHOR_ID=?,AUTHOR_NAME=?,DEPT_ID=?,DEPT_NAME=?,LAST_UPDATE_TIME=?,DESCRIPTION=?")
		.append(" where ID=? ");
		logger.debug("UpdateSQL:"+sql.toString());
		this.jdbcTemplate.batchUpdate(sql.toString(), new DocumentNoteBatchPreparedStatementForUpdate(docNotes));
	}

	@SuppressWarnings("unchecked")
	public List<DocumentNote> findNoteByFieldKey(String keyId, Integer workItemId) {
		String sql="select ID, WORKITEM_ID,DOC_ID, FIELD_KEY, VALUE, AUTHOR_ID, AUTHOR_NAME,DEPT_ID, DEPT_NAME, CREATE_TIME, LAST_UPDATE_TIME, DESCRIPTION,SCOPE "+
	     " from BPM_DC_NOTE where (FIELD_KEY,WORKITEM_ID)=(?,?)";
		logger.debug("QuerySQL:"+sql);
		return this.jdbcTemplate.query(sql, new Object[]{keyId,workItemId},new DocumentNoteRowMapper());
	}

	@SuppressWarnings("unchecked")
	public List<DocumentNote> findDocumentNoteList(String docId, String keyId) {
		String sql="select ID, WORKITEM_ID,DOC_ID, FIELD_KEY, VALUE, AUTHOR_ID, AUTHOR_NAME,DEPT_ID, DEPT_NAME, CREATE_TIME, LAST_UPDATE_TIME, DESCRIPTION,SCOPE "+
	     " from BPM_DC_NOTE where (FIELD_KEY,DOC_ID)=(?,?) order by LAST_UPDATE_TIME desc ";
		logger.debug("QuerySQL:"+sql);
		return this.jdbcTemplate.query(sql, new Object[]{keyId,docId},new DocumentNoteRowMapper());
	}

	@SuppressWarnings("unchecked")
	public List<DocumentNote> findNotesBySQL(String sql, Object... obj) {
		return this.jdbcTemplate.query(sql, obj, new DocumentNoteRowMapper());
	}

}
